//
//  PreferenceView.m
//  CarLifes
//
//  Created by yu on 2017/7/13.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "PreferenceView.h"

@interface PreferenceView (){

    UIView *_preferenceBtnsContainer;
}

@property (nonatomic, weak) UIButton *btn;
@property (nonatomic, strong) UIButton *avoidCongestion;
@property (nonatomic, strong) UIButton *avoidCost;
@property (nonatomic, strong) UIButton *avoidHighway;
@property (nonatomic, strong) UIButton *prioritiseHighway;

@end

@implementation PreferenceView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self buildPreferenceView];
    }
    return self;
}

- (void)buildPreferenceView
{
    UIView *backgroundView = [[UIView alloc] init];
    [self addSubview:backgroundView];
    
    UILabel *preferLabel = [[UILabel alloc] init];
    preferLabel.text = @"规划偏好";
    preferLabel.font = [UIFont systemFontOfSize:16];
    preferLabel.textAlignment = NSTextAlignmentLeft;
    [backgroundView addSubview:preferLabel];
    
    _preferenceBtnsContainer = [UIView new];
    _preferenceBtnsContainer.backgroundColor = [UIColor clearColor];
    [self addSubview:_preferenceBtnsContainer];
    
    NSArray *picNormalArray = @[@"avoid_crowd_normal",@"avoid_charge_normal",@"avoid_highway_normal",@"highway_priority_normal"];
    NSArray *picSelectArray = @[@"avoid_crowd_select",@"avoid_charge_select",@"avoid_highway_select",@"highway_priority_select"];
    
    NSMutableArray *temp = [NSMutableArray new];
    for (int i = 0; i < 4; i++) {
        UIButton *btn = [UIButton new];
        self.btn = btn;
        btn.tag = i + 1;
        [btn setImage:IMAGE_NAMED(picNormalArray[i]) forState:UIControlStateNormal];
        [btn setImage:IMAGE_NAMED(picSelectArray[i]) forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(preferenceClick:) forControlEvents:UIControlEventTouchUpInside];
        [_preferenceBtnsContainer addSubview:btn];
        btn.sd_layout.autoHeightRatio(1.0);
        [temp addObject:btn];
    }
    
    [_preferenceBtnsContainer setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:4 verticalMargin:0 horizontalMargin:10 verticalEdgeInset:0 horizontalEdgeInset:10];
    
    self.avoidCongestion = (UIButton *)[self viewWithTag:1];
    self.avoidCost = (UIButton *)[self viewWithTag:2];
    self.avoidHighway = (UIButton *)[self viewWithTag:3];
    self.prioritiseHighway = (UIButton *)[self viewWithTag:4];
    self.prioritiseHighway.selected = YES;
    
    // finish
    UIButton *finishBtn = [[UIButton alloc] init];
    [self addSubview:finishBtn];
//    finishBtn.backgroundColor = kBaseColor;
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    finishBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [finishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [finishBtn addTarget:self action:@selector(finishClick:) forControlEvents:UIControlEventTouchUpInside];
    
    if (@available(iOS 13.0, *)) {
        UIColor *bgColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return kBackGroundLightGrayColor;
            } else {
                return kRgb(60, 60, 62);
            }
        }];
        
        UIColor *finishBtnColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return kBaseColor;
            } else {
                return kBaseColorDark;
            }
        }];
        
        backgroundView.backgroundColor = bgColor;
        self.backgroundColor = [UIColor tertiarySystemBackgroundColor];
        preferLabel.textColor = UIColor.secondaryLabelColor;
        finishBtn.backgroundColor = finishBtnColor;
    } else {
        backgroundView.backgroundColor = kBackGroundLightGrayColor;
        self.backgroundColor = [UIColor whiteColor];
        preferLabel.textColor = kTextBlackColor;
        finishBtn.backgroundColor = kBaseColor;
    }
    
    backgroundView.sd_layout
    .leftSpaceToView(self, 0)
    .rightSpaceToView(self, 0)
    .topEqualToView(self)
    .heightIs(36);
    
    preferLabel.sd_layout
    .leftSpaceToView(backgroundView, 10)
    .rightEqualToView(backgroundView)
    .topEqualToView(backgroundView)
    .bottomEqualToView(backgroundView);
    
    finishBtn.sd_layout
    .rightSpaceToView(self, 10)
    .bottomSpaceToView(self, kIsIphoneX ? 34 : 15)
    .widthIs(70)
    .heightIs(40);
    finishBtn.sd_cornerRadius = @(1);
    
    _preferenceBtnsContainer.sd_layout
    .leftSpaceToView(self, 0)
    .rightSpaceToView(self, 0)
    .topSpaceToView(backgroundView, 12)
    .bottomSpaceToView(finishBtn, 12);

}

- (void)finishClick:(id)sender {
    if (_preferenceBlock) {
        _preferenceBlock();
    }
}

- (void)preferenceClick:(UIButton *)sender
{
    switch (sender.tag) {
        case 1:{
        [self changeButtonState:sender selected:!sender.selected];
        }
            break;
            
        case 2:{
            [self changeButtonState:sender selected:!sender.selected];
            if (sender.selected == YES) {
                [self changeButtonState:self.prioritiseHighway selected:NO];
            }
        }
            break;
            
        case 3:{
            [self changeButtonState:sender selected:!sender.selected];
            if (sender.selected == YES) {
                [self changeButtonState:self.prioritiseHighway selected:NO];
            }
        }
            break;
            
        case 4:{
            [self changeButtonState:sender selected:!sender.selected];
            if (sender.selected == YES) {
                [self changeButtonState:self.avoidCost selected:NO];
                [self changeButtonState:self.avoidHighway selected:NO];
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - Interface

- (AMapNaviDrivingStrategy)strategyWithMultiple
{
    return ConvertDrivingPreferenceToDrivingStrategy(YES,self.avoidCongestion.selected,self.avoidHighway.selected,self.avoidCost.selected,self.prioritiseHighway.selected);
}

#pragma mark - Handle Button

- (void)changeButtonState:(UIButton *)button selected:(BOOL)selected {
    button.selected = selected;
//    button.backgroundColor = button.selected ? [UIColor redColor] : [UIColor blackColor];
}

@end
